<?php
/**
 * 名称：CopyDBAction
 * 描述：数据拷贝
 * 作者：sun 时间：2013-11-01
 * 版本号：V1.0.0
 * ========================历史版本=================================
 * 序号	 版本号 	日期			姓名	 	描述
 * 1 	V1.0.0 	2013-11-01 	sun  	数据拷贝
 * ========================历史版本=================================
 * 说明：请在有更改程序的时候修改以上注释
 */
class CopyDBAction extends CommonAction {
	
	public function importData(){
		$this->display();
	}
	
	public function doImportData(){
		$username=$_POST['uname'];
		$password=$_POST['password'];
		$dbname=$_POST['dbname'];
		
		$tag = (PHP_OS==="WINNT")?",":":";
		$servername = $_POST['sname'].$tag.$_POST['dbport'];
		
		$conn=mssql_connect($servername,$username,$password);
		if(mssql_select_db($dbname,$conn)){
			$query=mssql_query("select  PublishmentSystemID as siteid,PublishmentSystemName as name,PublishmentSystemDir as dirname,
    			 PublishmentSystemUrl as domain from siteserver_PublishmentSystem");
			$data = array();
			while($result=mssql_fetch_array($query)){
				for ($i = 0; $i < count($result); $i++){
					unset($result[$i]);
				}
				if (PHP_OS==="WINNT"){
					foreach ($result as $k => $v){
						$result[$k] = iconv('GB2312', 'UTF-8', $v);
					}
				}
				$data[] = $result;
			}
			
// 			$expire   = 1 * 1800;
// 			S("mssql_servername",$servername);
// 			S("mssql_uname",$username);
// 			S("mssql_password",$password);
// 			S("mssql_dbname",$dbname);
			
			F("mssql_servername",$servername);
			F("mssql_uname",$username);
			F("mssql_password",$password);
			F("mssql_dbname",$dbname);
			
			mssql_close($conn);
			$this->assign("data",$data);
			$this->display();
		}else{
			$this->assign("error","连接数据库失败");
			$this->display("importData");
		}
	}
	
	public function beginImportData(){
		$sids = $_POST['ids'];
		if($sids == null || $sids == ""){
			echo 0;
			exit;
		}
		$servername = F("mssql_servername");
		$username=F("mssql_uname");
		$password=F("mssql_password");
		$dbname=F("mssql_dbname");
		
		
		$conn=mssql_connect($servername,$username,$password);
		if(mssql_select_db($dbname,$conn)){
			$sres = $this->importSite($sids);
			$nres = $this->importNode($sids);
			$cres = $this->importContent($sids);
		}
		mssql_close($conn);
		if($sres && $nres && $cres){
			echo 1;
		}else{
			echo 0;
		}
	}
    
    public function importSite($sids){
    	$query=mssql_query("select  PublishmentSystemID as siteid,
    				PublishmentSystemName as name,
    				PublishmentSystemDir as dirname,
    			 	PublishmentSystemUrl as domain,
    				IsHeadquarters as status  
    				from siteserver_PublishmentSystem where PublishmentSystemID in(".$sids.")");
    	if(mssql_num_rows($query)  < 1){
    		return true;
    	}
    	$data = array();
    	while($result=mssql_fetch_array($query)){
			for ($i = 0; $i < count($result); $i++){
				unset($result[$i]);
			} 
			foreach ($result as $k => $v){
				if (PHP_OS==="WINNT"){
					$result[$k] = iconv('GB2312', 'UTF-8', trim($v));
				}
				if ($k === "status"){
					$result[$k] = ($v ==="True")?1:0;
				}
			}
			$this->addSiteNode($result['name'],$result['siteid']);
    		$data[] = $result;
    	}
    	$model = new Model();
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_site change siteid  siteid int(11) not null ");
    	$res = M("cms_site") ->addAll($data);
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_site change siteid  siteid int(11) not null AUTO_INCREMENT");
    	if($res){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    public function importNode($sids){
    	$query=mssql_query("SELECT NodeID as id,convert(varchar(255),NodeName) as cname,
    			PublishmentSystemID as siteid,
    			ParentID as pid,
    			convert(varchar(255),ParentsPath) as plistid,
    			Taxis as display_order,
    			convert(varchar(255),AddUserName) as create_uname,
    			DATEDIFF(s,'1970-01-01 00:00:00',AddDate) as inputtime,
    			ImageUrl as imageurl,
    			convert(text,Content) as content,
    			ContentNum as content_num,
    			FilePath as createurl ,
    			ChannelFilePathRule as catenaming,
    			ContentFilePathRule as artnaming,
    			LinkUrl as url,
    			convert(varchar(255),Keywords) as keywords,
    			convert(text,Description) as description,
    			convert(varchar(255),NodeGroupNameCollection) as group_name_collection
       from siteserver_Node  where PublishmentSystemID in(".$sids.")");
    	if(mssql_num_rows($query) < 1){
    		return true;
    	}
    	$data = array();
    	while($result=mssql_fetch_array($query)){
    		for ($i = 0; $i < count($result); $i++){
    			unset($result[$i]);
    		}
    		foreach ($result as $k => $v){
    			if (PHP_OS==="WINNT"){
    				$result[$k] = addslashes(iconv('GB2312', 'UTF-8', trim($v)));
    			}
    			if($k === "inputtime"){
    				$result['updatetime'] = $v;
    			}else if($k === "create_uname"){
    				$result['update_uname'] = $v;
    			}
    		}
    		$data[] = $result;
    	}
    	$model = new Model();
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_category change id  id int(11) not null ");
    	$res = M("cms_category") ->addAll($data);
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_category change id  id int(11) not null AUTO_INCREMENT");
    	$this->importNodeDes($sids);
    	if($res){
    		return true;
    	}else{
    		return false;
    	}
    }
    
    private function importNodeDes($sids){
    	$sqlstr .= "select smin.NodeID as id,smin.PublishmentSystemID as siteid, convert(varchar(255),sm.PageTitle) as pagetitle,convert(varchar(255),sm.Description) as pagedescription,convert(varchar(255),sm.Keywords) as keywords
 					 from siteserver_SeoMetasInNodes smin left join siteserver_SeoMeta sm on smin.SeoMetaID=sm.SeoMetaID
 					 where smin.IsChannel='True' ";
    	if (!empty($sids)) {
    		$sqlstr .= ' and smin.PublishmentSystemID = '.$sids;
    	}
    	$data = mssql_query($sqlstr);
    	while($result=mssql_fetch_array($data)){
    		for ($i = 0; $i < count($result); $i++){
    			unset($result[$i]);
    		}
    		$res = M('cms_category') ->save($result);
    	}
    }
    
    
    
    private function importNodeDesForBaobao(){
    	$username= 'yonglang0329';
    	$password= 'yonglang20100703^%$#@!';
    	$dbname= 'i8cms';
    	
    	$tag = (PHP_OS==="WINNT")?",":":";
    	$servername = '121.197.2.12'.$tag.'1433';
    	$conn=mssql_connect($servername,$username,$password);
    	if(mssql_select_db($dbname,$conn)){
//     		$this->importContent("71");
//     		$this->addSiteNode("宝宝","71");
			$this->importRelateArticle();
    	}
    	mssql_close($conn);
    }
    
    private function importRelateArticleByEXEC(){
    	return;
    	$ids = M('cms_article') -> where('id > 28188') ->field('id')->select();
    	$related = array();
    	foreach ($ids as $k => $v){
    		$contentid =  $v['id'];
    		var_dump('content id = '.$contentid.'<br>') ;
    		$sqlstr = " exec [UP_GetContentrelatedList2] '".$contentid."' ";
    		$data = mssql_query($sqlstr);
    		while ($result = mssql_fetch_array($data)){
    			$relatedarticle = array();
    			for ($i = 0; $i < count($result); $i++){
    				unset($result[$i]);
    			}
    			$relatedarticle['id'] = $contentid;
    			$relatedarticle['rurl'] = $result['Url'];
    			$relatedarticle['rtitle'] = $result['Title'];
    			$relatedarticle['siteid'] = '71';
    			preg_match("/\/([1-9][0-9]*)\/([1-9][0-9]*)\.html/", $relatedarticle['rurl'],$maches);
    			$relatedarticle['rcid'] = intval($maches[1]);
    			$relatedarticle['rid'] = intval($maches[2]);
    			if (strpos($relatedarticle['rurl'],'i8i8i8.com/baby') > 0) {
    				$related[] = $relatedarticle;
    				if (count($related) == 500) {
    					M('cms_relatedarticle') -> addAll($related);
    					$related = array();
    				}
    			}
    		}
    		mssql_free_result($data);
    	}
    	M('cms_relatedarticle') -> addAll($related);
    }
    
    private function importRelateArticle(){
    	$sqlstr = "select  r.ID as id,r.Url as rurl,c.PublishmentSystemID as siteid,
    			convert(varchar(255),r.Title) as rtitle 
    			from ".$this->_dbPrefix."ContentrelatedList r left join au_Content c on r.ID=c.ID where c.PublishmentSystemID=71 and c.ID > 28188";
    	$data = mssql_query($sqlstr);
    	$related = array();
    	while ($result = mssql_fetch_array($data)){
    		for ($i = 0; $i < count($result); $i++){
    			unset($result[$i]);
    		}
    		preg_match("/\/([1-9][0-9]*)\/([1-9][0-9]*)\.html/", $result['rurl'],$maches);
    		$result['rcid'] = intval($maches[1]);
    		$result['rid'] = intval($maches[2]);
    		if (strpos($result['rurl'],'i8i8i8.com/baby') > 0) {
    			$related[] = $result;
    			if (count($related) == 500) {
    				M('cms_relatedarticle') -> addAll($related);
    				$related = array();
    			}
    		}
    	}
    	mssql_free_result($data);
    	echo 'related article count is '.count($related);
    	$res = M('cms_relatedarticle') -> addAll($related);
    	echo 'cms related article num is'.$res;
    }
    
    public function importContent($sids){
    	$maxRow = mssql_query("select max(ID) from au_Content where PublishmentSystemID in(".$sids.")");
      
    	while($max=mssql_fetch_array($maxRow)){
    		$maxId = $max[0];
    	}
    	if(empty($maxId)){
    		return true;
    	}
    	$begin = 1;
    	$end = 200;
    	$model = new Model();
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_article change id  id int(11) not null ");
    	while($end < $maxId+200){
    		$query=mssql_query("SELECT ID as id
						      ,NodeID as cid
						      ,PublishmentSystemID as siteid
						      ,convert(varchar(255),AddUserName) as create_uname
						      ,convert(varchar(255),LastEditUserName) as update_uname
						      ,DATEDIFF(s,'1970-01-01 00:00:00',LastEditDate) as updatetime
						      ,Taxis as display_order
						      ,IsChecked as status
						      ,convert(varchar(255),Title) as title
						      ,convert(varchar(255),SubTitle) as subtitle
						      ,ImageUrl as imageurl
						      ,LinkUrl as url
						      ,convert(varchar(255),Author) as author
						      ,convert(varchar(255),Source) as source
						      ,FileUrl as fileurl
						      ,IsRecommend as is_recommend
						      ,IsHot as is_hot
						      ,IsColor as is_color
						      ,IsTop as  is_top
    						  ,convert(text,Summary) as summary
						      ,convert(text,Content) as content
						      ,DATEDIFF(s,'1970-01-01 00:00:00',AddDate) as inputtime
						      ,Hits as readpoint
						      ,convert(varchar(255),Description) as description
						      ,convert(varchar(255),keywords) as keywords
    						  ,convert(varchar(255),IsIndexNews) as is_index_news
						      ,convert(varchar(255),Tags) as tags
						      ,convert(varchar(255),BigImageUrl) as big_imageurl
						      ,convert(varchar(255),ContentGroupNameCollection) as group_name_collection
    						  ,convert(text,RelatedNodeList) as related_node_list
						  FROM au_Content where PublishmentSystemID in(".$sids.") and ID between ".$begin." and ".$end);
    		$data = array();
    		while($result=mssql_fetch_array($query)){
    			for ($i = 0; $i < count($result); $i++){
    				unset($result[$i]);
    			}
    			foreach ($result as $k => $v){
    				if (PHP_OS==="WINNT"){
    					$result[$k] = addslashes(iconv('GB2312', 'UTF-8//IGNORE',trim($v)));
    				}
    				if($k === "status" ||$k === "is_recommend" ||$k === "is_hot" ||$k === "is_color" ||$k === "is_top"){
    					$result[$k] = ($v ==="True")?1:0;
    				}
    			}
    			$data[] = $result;
    			unset($result);
    		}
    		mssql_free_result($query);
    		
    		$res = M("cms_article") ->addAll($data);
    		unset($data);
    		$begin += 200;
    		$end += 200;
    	}
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_article change id  id int(11) not null AUTO_INCREMENT");
    	unset($model);
    	if($res){
    		return true;
    	}else{
    		return false;
    	}
    }

    /**
     * 导入宝宝新数据  控制器
     * 
     */
    public function beginImportnewData(){
    	$servername = F("mssql_servername");
    	$username=F("mssql_uname");
    	$password=F("mssql_password");
    	$dbname=F("mssql_dbname");
    	$conn=mssql_connect($servername,$username,$password);
    	if(mssql_select_db($dbname,$conn)){
    		$cres = $this->importnewContent();
    	}
    	mssql_close($conn);
    	if($cres){
    		echo 1;
    	}else{
    		echo 0;
    	}
    }
   /**
    * 导入宝宝站点新数据
    * @author jepin
    * @date 2141107
    * @return boolean
    */
    public function importnewContent(){
    	$sids ='71';
    	$maxRow = mssql_query("select max(ID) from au_Content where PublishmentSystemID in(".$sids.")");
    	$c =M()->query("SELECT MAX(id) as max from ".$this->_dbPrefix."cms_article where siteid  in(".$sids.")");
    	$mmax =$c[0]['max']+1;
    	
    	while($max=mssql_fetch_array($maxRow)){
    		$maxId = $max[0];
    	}
    	if(empty($maxId)){
    		return true;
    	}
    	$begin = $mmax;
    	$end = $mmax+200;
    	$model = new Model();
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_article change id  id int(11) not null ");
    	while($end < $maxId+200){
    		$query=mssql_query("SELECT ID as id
						      ,NodeID as cid
						      ,PublishmentSystemID as siteid
						      ,convert(varchar(255),AddUserName) as create_uname
						      ,convert(varchar(255),LastEditUserName) as update_uname
						      ,DATEDIFF(s,'1970-01-01 00:00:00',LastEditDate) as updatetime
						      ,Taxis as display_order
						      ,IsChecked as status
						      ,convert(varchar(255),Title) as title
						      ,convert(varchar(255),SubTitle) as subtitle
						      ,ImageUrl as imageurl
						      ,LinkUrl as url
						      ,convert(varchar(255),Author) as author
						      ,convert(varchar(255),Source) as source
						      ,FileUrl as fileurl
						      ,IsRecommend as is_recommend
						      ,IsHot as is_hot
						      ,IsColor as is_color
						      ,IsTop as  is_top
    						  ,convert(text,Summary) as summary
						      ,convert(text,Content) as content
						      ,DATEDIFF(s,'1970-01-01 00:00:00',AddDate) as inputtime
						      ,Hits as readpoint
						      ,convert(varchar(255),Description) as description
						      ,convert(varchar(255),keywords) as keywords
    						  ,convert(varchar(255),IsIndexNews) as is_index_news
						      ,convert(varchar(255),Tags) as tags
						      ,convert(varchar(255),BigImageUrl) as big_imageurl
						      ,convert(varchar(255),ContentGroupNameCollection) as group_name_collection
    						  ,convert(text,RelatedNodeList) as related_node_list
						  FROM au_Content where PublishmentSystemID in(".$sids.") and ID between ".$begin." and ".$end);
    		$data = array();
    		while($result=mssql_fetch_array($query)){
    			for ($i = 0; $i < count($result); $i++){
    				unset($result[$i]);
    			}
    			foreach ($result as $k => $v){
    				if (PHP_OS==="WINNT"){
    					$result[$k] = addslashes(iconv('GB2312', 'UTF-8//IGNORE',trim($v)));
    				}
    				if($k === "status" ||$k === "is_recommend" ||$k === "is_hot" ||$k === "is_color" ||$k === "is_top"){
    					$result[$k] = ($v ==="True")?1:0;
    				}
    			}
    			$data[] = $result;
    			unset($result);
    		}
    		mssql_free_result($query);
    
    		$res = M("cms_article") ->addAll($data);
    		unset($data);
    		$begin += 200;
    		$end += 200;
    	}
    	$model->execute("alter table ".C ( 'DB_PREFIX' ) ."cms_article change id  id int(11) not null AUTO_INCREMENT");
    	unset($model);
    	$c =M()->query("SELECT MAX(id) as max from ".$this->_dbPrefix."cms_article where siteid  in(".$sids.")");
    	$mmaxr =$c[0]['max'];
    	if($mmaxr==$maxId){
    		return true;
    		
    	}else{
    		return false;
    	}
    }
    
    public function test(){
    	$this->addSiteNode('爱吧网','1');
//     	$this->addSiteNode('结婚','30');
    	$this->addSiteNode('宝宝','71');
    	$this->addSiteNode('关于','940');
//     	$this->addSiteNode('音乐','1460');
//     	$this->addSiteNode('家活','1484');
//     	$this->addSiteNode('旅游','1487');
//     	$this->addSiteNode('资讯','1761');
//     	$this->addSiteNode('友情链接','1765');
//     	$this->addSiteNode('教育','1777');
    	$this->addSiteNode('涌浪官网','3968');	
    }
    
    public function addSiteNode($sitename,$sid){
		$sitename = $sitename;
    	$sitenode = array('name'=>'cms','title'=>$sitename,'status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>0,'level'=>1,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$modulenodepid = M('node') -> add($sitenode);
		
		$modulenode = array('name'=>'Site','title'=>'站点管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'index','title'=>'站点管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Article','title'=>'内容管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'contentlist','title'=>'内容列表','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionnode[] = array('name'=>'delArticle','title'=>'内容管理_删除','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
        $actionnode[] = array('name'=>'oper','title'=>'审核和生成','status'=>'1','remark'=>NULL,'sort'=>NULL,
                'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Cate','title'=>'栏目管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'sitecatelist','title'=>'栏目管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionnode[] = array('name'=>'delCate','title'=>'栏目管理_删除','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Template','title'=>'模板管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'sitetemplatelist','title'=>'模板管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionnode[] = array('name'=>'deletetpl','title'=>'模板删除','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Create','title'=>'生成管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'create_home','title'=>'生成首页','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionnode[] = array('name'=>'create_cate','title'=>'生成栏目页','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionnode[] = array('name'=>'create_content','title'=>'生成内容页','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Group','title'=>'分组管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'sitegrouplist','title'=>'内容分组管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionnode[] = array('name'=>'sitegrouplist','title'=>'栏目分组管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'BlockClassify','title'=>'区块分类管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'classify','title'=>'区块分类','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Config','title'=>'配置管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'siteconfiglist','title'=>'配置管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Config','title'=>'云配置管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'yunconfig','title'=>'又拍云配置','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
		
		$modulenode = array('name'=>'Article','title'=>'回收站管理','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$modulenodepid,'level'=>2,'type'=>0,'group_id'=>0,'sid'=>$sid);
		$actionpids = M('node') -> add($modulenode);
		$actionnode[] = array('name'=>'deletecontentlist','title'=>'回收站','status'=>'1','remark'=>NULL,'sort'=>NULL,
				'pid'=>$actionpids,'level'=>3,'type'=>0,'group_id'=>0,'sid'=>$sid);
			
		M('node') -> addAll($actionnode);
	}
	//导入站内链接
	public function importLinkUrl(){
		$username= 'yonglang0329';
		$password= 'yonglang20100703^%$#@!';
		$dbname= 'i8cms';
		$tag = (PHP_OS==="WINNT")?",":":";
		$servername = '121.197.2.12'.$tag.'1433';
		$conn=mssql_connect($servername,$username,$password);
		if(mssql_select_db($dbname,$conn)){
			$query=mssql_query("select InnerLinkName,PublishmentSystemID,LinkUrl from siteserver_InnerLink where PublishmentSystemID =71 ");
			$related = array();
			while ($result = mssql_fetch_array($query)){
				for ($i = 0; $i < count($result); $i++){
					unset($result[$i]);
				}
				$related[] = $result;
			}
		}
		$data=array();
		foreach ($related as $v) {
			$data['text']=iconv("GB2312","UTF-8",$v['InnerLinkName']); 
			$data['url'] =$v['LinkUrl'];
			M('textlink')->add($data);
			unset($data);
		}
		mssql_close($conn);
	}
	//导入评论数据
	public function importComments(){
		$username= 'yonglang0329';
		$password= 'yonglang20100703^%$#@!';
		$dbname= 'i8cms';
		$tag = (PHP_OS==="WINNT")?",":":";
		$servername = '121.197.2.12'.$tag.'1433';
		$conn=mssql_connect($servername,$username,$password);
		if(mssql_select_db($dbname,$conn)){
			
			$query=mssql_query("select   CommentID,ContentID, UserName,AddUserID,ID,IPAddress  ,DATEDIFF(s,'1970-01-01 00:00:00',Adddate) as adddate ,convert(text,[Content]) as content from siteserver_commentcontent where PublishmentSystemID =71");
			dump($query);
			$related = array();
			while ($result = mssql_fetch_array($query)){
				foreach ($result as $k => $v){
					if (PHP_OS==="WINNT"){
						$result[$k] = addslashes(iconv('GB2312', 'UTF-8//IGNORE',trim($v)));
					}
				}
				for ($i = 0; $i < count($result); $i++){
					unset($result[$i]);
				}
				$related[] = $result;
			}
		}
		$data=array();
		foreach ($related as $v) {
			$data['id'] =$v['ID'];
			$data['parent'] =$v['ID'];
			$data['comment_id'] = $v['CommentID'];
			$data['author'] = $v['UserName'];
			$data['user_id'] =$v['AddUserID'];
			$data['article_id'] =$v['ContentID'];
			$data['author_ip'] = $v['IPAddress'];
			$data['content'] =$v['content'];
			$data['commenttime'] = $v['adddate'];
			M('cms_comments')->add($data);
			//echo M()->getLastSql().'<br/>';
			unset($data);
		}
		mssql_close($conn);
	}
	/**
	 * 评论文章数量矫正
	 */
	public function  toright(){
		$sql = 'SELECT  article_id, COUNT(*) as count  from  '.$this->_dbPrefix.'cms_comments  t GROUP  BY article_id';
		
		$data =M()->query($sql);
		foreach ($data as $v){
			$sqlu='update   '.$this->_dbPrefix.'cms_article  set comment_count ='.$v['count'].' where  id= '.$v['article_id'];
			echo $sqlu.'<br/>';
			M()->query($sqlu);
		}
	}
	
}
?>